package com.record_video;

import java.util.List;

import com.oprekTA.R;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class RecordVideo_activity extends Activity {
	private final String TAG = "Preview oprekTA";
	
	private Camera mCamera;
	private int mCameraID=-99;
	private int numberOfCameras;
	private CameraPreview mPreview;
	private FrameLayout preview;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.record_video);
        preview = (FrameLayout) findViewById(R.id.camera_preview);

        //mCamera = getCameraInstance();
        //mPreview.setCameraDisplayOrientation(this,mCameraID,mCamera);
        // Create our Preview view and set it as the content of our activity
        mPreview = new CameraPreview(this);
        
        preview.addView(mPreview);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        
        mCamera = getCameraInstance();
        mPreview.setCameraDisplayOrientation(this,mCameraID,mCamera);
        mPreview.setCamera(mCamera);
        mCamera.startPreview();
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Because the Camera object is a shared resource, it's very
        // important to release it when the activity is paused.
        if (mCamera != null) {
        	mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
           super.onSaveInstanceState(outState);
           //save the relevant information
           outState.putInt("cameraID", mCameraID+1);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
           super.onRestoreInstanceState(savedInstanceState);
           //restore the relevant information
           mCameraID = savedInstanceState.getInt("cameraID")-1;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.layout.menu_record_video, menu);
    	return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	// check if device can switch camera
    	int numberOfCamera = Camera.getNumberOfCameras();
    	if (numberOfCamera==1) {
    		MenuItem item = (MenuItem) findViewById(R.id.switch_camera);
    		item.setEnabled(false);
    	}
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// handle item selection
    	switch (item.getItemId()) {
    		case R.id.switch_camera :
    			switchCamera();
    			return true;
    		default:
    			return super.onOptionsItemSelected(item);
    	}
    }
    
    private void switchCamera() {
    	// OK, we have multiple cameras.
        // Release this camera -> cameraCurrentlyLocked
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }

        // Acquire the next camera and request Preview to reconfigure
        // parameters.
        mCameraID = (mCameraID + 1)
                % numberOfCameras;
        mCamera = getCameraInstance();
        mPreview.setCameraDisplayOrientation(this,mCameraID,mCamera);
        mPreview.setCamera(mCamera);
        // Start the preview
        mCamera.startPreview();
    }
    
    /** A safe way to get an instance of the Camera object. */
    public Camera getCameraInstance(){
        Camera c = null;
        try {
        	numberOfCameras = Camera.getNumberOfCameras();
        	if (mCameraID==-99) {
	        	if (numberOfCameras>1)
	        		mCameraID = 1;
	        	else
	        		mCameraID = 0;
        	}
        	c = Camera.open(mCameraID);
        }
        catch (Exception e){
            Log.d(TAG, "Error getCameraInstance : "+e.getMessage());
        }
        return c; // returns null if camera is unavailable
    }
    
}
